/*
 * Created by JFormDesigner on Wed Nov 20 12:23:53 CST 2024
 */

package com.hafu.view.ShangPin;

import com.hafu.model.SpInfo;
import com.hafu.service.SpInfoService;
import com.mysql.jdbc.StringUtils;
import sun.applet.Main;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;
import java.awt.*;
import java.math.BigDecimal;
import javax.swing.JFrame;

/**
 * @author aa
 */
public class AddShangPinFrm extends JFrame {
    private SpInfoService spInfoService = new SpInfoService();
    public AddShangPinFrm() {
        initComponents();
    }

    private void button1(ActionEvent e) {
        // TODO add your code here 添加商品
        String name = textField1.getText();
        if(StringUtils.isNullOrEmpty(name)){
            JOptionPane.showMessageDialog(null,"商品名称不能为空");
            return;
        }
        if(StringUtils.isNullOrEmpty(textField2.getText())){
            JOptionPane.showMessageDialog(null,"商品进价不能为空");
            return;
        }
        if(StringUtils.isNullOrEmpty(textField3.getText())){
            JOptionPane.showMessageDialog(null,"商品售价不能为空");
            return;
        }
        BigDecimal price = new BigDecimal(textField2.getText());
        BigDecimal sellPrice = new BigDecimal(textField3.getText());
        SpInfo spInfo = new SpInfo(name,price,sellPrice);
        Boolean aBoolean = spInfoService.addSpInfo(spInfo);
        if(aBoolean){
            JOptionPane.showMessageDialog(null,"添加成功");
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(null,"添加失败");
        }
    }

    private void button7(ActionEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        button7 = new JButton();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5546\u54c1");
        Container contentPane = getContentPane();

        //---- button7 ----
        button7.setText("\u63d0\u4ea4");
        button7.addActionListener(e -> {
			button1(e);
			button7(e);
		});

        //---- label1 ----
        label1.setText("\u5546\u54c1\u540d\u5b57");

        //---- label2 ----
        label2.setText("\u5546\u54c1\u8fdb\u4ef7");

        //---- label3 ----
        label3.setText("\u5546\u54c1\u552e\u4ef7");

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(107, 107, 107)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 127, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                        .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                        .addComponent(textField1, GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE))
                    .addContainerGap(108, Short.MAX_VALUE))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(183, 183, 183)
                    .addComponent(button7, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(242, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(25, 25, 25)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(44, 44, 44)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(42, 42, 42)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                    .addComponent(button7)
                    .addGap(40, 40, 40))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JButton button7;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
